import {
	$http
} from '@escook/request-miniprogram'
// import { baseUrl } from '@/utils/index.js'
let token = ''

//从网络请求包中引入$http请求对象
uni.$http = $http

/*#ifdef APP-PLUS*/
// $http.baseUrl = 'http://175.24.229.44:8084/backSystem'
$http.baseUrl = 'http://115.29.213.249/backSystem'
/*#endif*/

/*#ifdef H5*/
$http.baseUrl = '/backSystem'
/*#endif*/






//配置网络请求基础地址，因为uni.$http为$http的引用，所以可以直接修改$http对象下的属性
$http.beforeRequest = async function(options) {

	options.header = {
		// 'token': uni.getStorageSync('token'),
		'Authorization': uni.getStorageSync('token')
		// 'Authorization': 'Bearer b2FEbTFjWjEvSzhpL2NzSkhneCtEdz09OjcxODRkZjgyLWViMTYtNGZhOC05NjI0LWRjM2IzYzFmZDViZg=='
	}

	if (options.data.loading == 1) {
		delete options.data.loading
		uni.showLoading({
			title: 'Loading...'
		})
	}
}

$http.afterRequest = function(e) {
	//配置网络请求完毕后的拦截功能
	uni.hideLoading()
}

// 封装请求方法
export async function service(data, headers = 0) {

	return new Promise(async (resolve, reject) => {
		let res = null
		if (data.method.toLowerCase() === 'post') {
			res = await uni.$http.post(data.url, data.data);
		} else if (data.method.toLowerCase() === 'get') {
			res = await uni.$http.get(data.url);
		}

		if (res.data.code == '200') {
			resolve(res.data)
		} else {
			

			if (res.data.code == '401') {
				uni.setStorageSync('token', '')
				uni.setStorageSync('phone', '')
			}

			// uni.showToast({
			// 	title: res.data.msg || res.data.message,
			// 	duration: 2000,
			// 	icon: 'none'
			// });
			reject(res)
		}
	})
}
